package com.das.mechanic_base.mvp.view.create.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.das.mechanic_base.R;
import com.das.mechanic_base.R2;
import com.das.mechanic_base.adapter.main.X3ServiceListItemAdapter;
import com.das.mechanic_base.base.X3BaseViewPagerLazyFragment;
import com.das.mechanic_base.bean.main.HomeBodyItemBean;
import com.das.mechanic_base.bean.main.ReceiveStatusBean;
import com.das.mechanic_base.mvp.contract.create.X3ServiceListContract;
import com.das.mechanic_base.mvp.presenter.create.X3ServiceListPresenter;
import com.das.mechanic_base.mvp.view.create.X3ServiceInfoActivity;
import com.das.mechanic_base.mvp.view.create.X3ServiceListActivity;
import com.das.mechanic_base.utils.X3StringUtils;
import com.das.mechanic_base.utils.X3Utils;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;

public class X3ServiceListFragment extends X3BaseViewPagerLazyFragment<X3ServiceListPresenter> implements X3ServiceListContract.View,
        X3ServiceListItemAdapter.IOnClickHomeWork {

    @BindView(R2.id.sl_view)
    SmartRefreshLayout sl_view;
    @BindView(R2.id.rlv_item)
    RecyclerView rlv_item;

    private ReceiveStatusBean home_body;
    private int bodyIndex = 1;
    private X3ServiceListItemAdapter serviceAdapter;

    private int currentIndex;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_home_body_item;
    }

    public static X3ServiceListFragment newInstance(ReceiveStatusBean statusBean, int currentIndex) {
        X3ServiceListFragment fragment = new X3ServiceListFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("HOME_ITEM", statusBean);
        bundle.putInt("POSITION",currentIndex);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected void initView(View view) {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        linearLayoutManager.setInitialPrefetchItemCount(5);
        rlv_item.setLayoutManager(linearLayoutManager);
        sl_view.setEnableHeaderTranslationContent(false);
        sl_view.setEnableLoadMore(true);
        sl_view.setOnLoadMoreListener(refreshLayout -> {
            sl_view.finishLoadMore(500);
            this.bodyIndex++;
            requestHomeBody();
        });

        sl_view.setOnRefreshListener(refreshlayout -> {
            refreshTop();
        });

    }

    @Override
    public void getBundle(Bundle bundle) {
        home_body = (ReceiveStatusBean) bundle.getSerializable("HOME_ITEM");
        currentIndex = bundle.getInt("POSITION", -1);
        bodyIndex = 1;
    }

    @Override
    protected void lazyInit() {
        serviceAdapter = new X3ServiceListItemAdapter(getContext(),home_body.statusName);
        rlv_item.setAdapter(serviceAdapter);
        serviceAdapter.setiOnClickHomeWork(this);
    }

    private void refreshTop() {
        sl_view.finishRefresh(500);
        this.bodyIndex = 1;
        if (getActivity() != null) {
            X3ServiceListActivity activity = (X3ServiceListActivity) getActivity();
            activity.requestReceiveList(currentIndex);
        }
    }

    private void requestHomeBody() {
        Map<String,Object> map = new HashMap<>();
        map.put("pageNum",bodyIndex);
        map.put("pageSize",20);

        if (mPresenter != null) {
            mPresenter.requestReceiveDetailList(map,home_body.id);
        }
    }

    @Override
    protected X3ServiceListPresenter createPresenter() {
        return new X3ServiceListPresenter();
    }

    @Override
    protected void everyInit() {
        if (sl_view != null) {
            sl_view.finishRefresh();
        }
        requestHomeBody();
    }

    @Override
    public void getReceiveStatusSuccess(List<ReceiveStatusBean> mList,int serviceIndex) {

    }


    @Override
    public void getReceiveDetailListSuccess(List<HomeBodyItemBean.ListBean> mList) {
        if (serviceAdapter == null) {
            return;
        }
        if (bodyIndex == 1) {
            serviceAdapter.changeAdapter(mList);
        }else {
            serviceAdapter.changeAddAdapter(mList);
            if (X3StringUtils.isListEmpty(mList)) {
                sl_view.finishLoadMoreWithNoMoreData();
            }
        }
    }

    @Override
    public void iOnClickWorkGetCar(long id) {
        if (!X3Utils.isFastClick()) {
            return;
        }
        Intent intent = new Intent(getActivity(), X3ServiceInfoActivity.class);
        intent.putExtra("receiveBaseId",id);
        startActivity(intent);
    }

}
